import Foundation

enum AppEnvironmentKind: String {
    case debug
    case staging
    case production
}

struct AppConfig {
    let kind: AppEnvironmentKind
    let baseURL: URL

    static func make() -> AppConfig {
        // 1) 运行时覆盖（Scheme 环境变量优先）
        if let envURL = ProcessInfo.processInfo.environment["APP_BASE_URL"],
           let url = URL(string: envURL) {
            return AppConfig(kind: AppConfig.compileTimeKind, baseURL: url)
        }

        // 2) Info.plist 覆盖（可选）
        if let dict = Bundle.main.infoDictionary,
           let urlString = dict["APP_BASE_URL"] as? String,
           let url = URL(string: urlString) {
            return AppConfig(kind: AppConfig.compileTimeKind, baseURL: url)
        }

        // 3) 编译期默认
        return AppConfig(kind: AppConfig.compileTimeKind, baseURL: AppConfig.compileTimeBaseURL)
    }

    private static var compileTimeKind: AppEnvironmentKind {
        #if DEBUG
        return .debug
        #elseif STAGING
        return .staging
        #else
        return .production
        #endif
    }

    private static var compileTimeBaseURL: URL {
        #if DEBUG
        return URL(string: "https://api-debug.example.com")!
        #elseif STAGING
        return URL(string: "https://api-staging.example.com")!
        #else
        return URL(string: "https://api.example.com")!
        #endif
    }
}


